package com.eece496.Database;
import java.sql.*;

public class TeachingAssistant extends Database{

	private int TaID;
	private String loginName = null;
	private String password = null;
	private String firstName = null;
	private String lastName = null;
	private String email = null;
	
	public TeachingAssistant()
	{
		tableName = "TeachingAssistant";
	}
	
	public TeachingAssistant(int TaID, String loginName, String password, String firstName, String lastName, String email)
	{
		tableName = "TeachingAssistant";
		
		this.TaID = TaID;
		this.loginName = loginName;
		this.password = password;
		this.firstName = firstName;
		this.lastName = lastName;
		this.email = email;
	}
	
	public String GetPassword()
	{
		return this.password;
	}
	
	public int GetID()
	{
		return this.TaID;
	}
    //@assignable stmt,sqlString;
	//@signals_only RuntimeException, IOException, SQLException;
	public boolean UserExists(int id) throws SQLException
	{
		Statement stmt = connection.createStatement();
		String sqlString = "select * from " + tableName + " where TaID = " + id;
		ResultSet rs = stmt.executeQuery(sqlString);
		
		if (rs.next())//check if user exists in database
		{
			this.TaID = Integer.parseInt(rs.getString("TaID"));
			this.loginName = rs.getString("loginName");
			this.password = rs.getString("password");
			this.firstName = rs.getString("firstname");
			this.lastName = rs.getString("lastname");
			this.email = rs.getString("email");
			return true;
		}
		else
		{
			return false;
		}
				
	}

	public boolean UserExists(String loginName) throws SQLException{
		
		Statement stmt = connection.createStatement();
		
		String sqlString = "select * from " + tableName + " where loginName = " + AddQuotes(loginName);
		
		ResultSet rs = stmt.executeQuery(sqlString);
		
		if (rs.next())//check if user exists in databse
		{
			this.TaID = Integer.parseInt(rs.getString("TaID"));
			this.loginName = rs.getString("loginName");
			this.password = rs.getString("password");
			this.firstName = rs.getString("firstname");
			this.lastName = rs.getString("lastname");
			this.email = rs.getString("email");
			return true;
		}
		else
		{
			return false;
		}
		
	
	}
	/*
        //@requires loginName !== Null;
    //@assignable newLoginName,TaID;
	//@signals_only RuntimeException, IOException,SQLException;
	public void ModPassword(String newLoginName, int tupleID) throws SQLException
	{
		super.Modify(newLoginName, "loginName","TaID",tupleID); 
	}
	//@requires password !== Null;
    //@assignable newPassword,TaID;
	//@signals_only RuntimeException, IOException,SQLException;
	public void ModPassword(String newPassword, int tupleID) throws SQLException
	{
		super.Modify(newPassword, "password","TaID",tupleID); 
	}
	//@requires firstname !== Null;
    //@assignable newFirstname,TaID;
	//@signals_only RuntimeException, IOException,SQLException;
	public void ModFirstName(String newFirstName, int tupleID) throws SQLException
	{
		super.Modify(newFirstName, "firstname","TaID",tupleID); 
	}
	*/
	//@requires  lastname !== Null;
    //@assignable newLastname,TaID;
	//@signals_only RuntimeException, IOException,SQLException;
	public void ModLastName(String newLastName, int tupleID) throws SQLException
	{
		super.Modify(newLastName, "lastname","TaID",tupleID); 
	}
	//@requires email !== Null;
    //@assignable newEmail,TaID;
	//@signals_only RuntimeException, IOException,SQLException;
	public void ModEmail(String newEmail, int tupleID) throws SQLException
	{
		super.Modify(newEmail, "email","TaID",tupleID); 
	}
	//@requires phone !== Null;
    //@assignable stmt,sqlString;
	//@signals_only RuntimeException, IOException,SQLException;
	@Override
	public void Insert() throws SQLException 
	{
		Statement stmt = connection.createStatement();
				
	    String sqlString = "INSERT INTO " + tableName + " VALUES (" + 
	    					TaID + ", " +
						AddQuotes(loginName) + ", " +
	    					AddQuotes(password) + ", " +
	    					AddQuotes(firstName) + ", " +
	    					AddQuotes(lastName) + ", " +
	    					AddQuotes(email)+  
	    					")";
	    
	    stmt.executeUpdate(sqlString);
	        
	    System.out.println(sqlString + " complete");
	}
	
	@Override
    //@assignable stmt,id,sqlString;
	//@signals_only RuntimeException, IOException,SQLException;
	public void Delete(String primaryKey) throws SQLException 
	{
		Statement stmt = connection.createStatement();
		int id = Integer.parseInt(primaryKey);
		String sqlString = "DELETE from " + tableName + " WHERE TaID = " + id;
		stmt.executeUpdate(sqlString);
		System.out.println(sqlString + " complete");
	}
	
	@Override
    //@assignable sqlString, stmt, ResultSet;
	//@signals_only RuntimeException, IOException,SQLException;
	public void Display() throws SQLException 
	{
	    String sqlString = "SELECT * FROM " + tableName;
	    Statement stmt = connection.createStatement();

	    ResultSet rs = stmt.executeQuery(sqlString);

	    while (rs.next())
	    {
	        System.out.println
	        (
	            rs.getString("TaID") + " " +
		    rs.getString("loginName") + " " +
	            rs.getString("password") + " " +
	            rs.getString("firstname") + " " +
	            rs.getString("lastname") + " " +
	            rs.getString("email") + " " 
	        );
	    }	
	}

	public String getFirstName() {
		// TODO Auto-generated method stub
		return this.firstName;
	}
	
	public String getLastName(){
		return this.lastName;
	}

}